/**
 * 设置信息表单页面
 * wangmei
 */
import React, { Component } from 'react';
import { Modal, Button, Form, Input, message, Select, Row, Col } from 'antd';
import * as Request from '../../../Server/Upgrade';
import qs from 'qs';

import {REQUEST_SUCCESS,REQUEST_UNAUTHORIZED} from '../../../common/const';
const FormItem = Form.Item;
const Option = Select.Option;

class ConditionTableFrom extends Component{
    constructor(props) {
        super(props);
    }

    //保存方法
    handleSubmit = (e) => {
        e.preventDefault();
        const { form, row, id, type, current, size } = this.props
        let itemState = type=='popupInfoCondition';
        form.validateFieldsAndScroll((err, values) => {
            if(!err){
                if(row.id==undefined){ //新增
                    if(itemState){ //泡泡条件
                        values.popupId = id;
                        values.checkPath = values.checkPath.replace(/\s*/g,"");
                        const _param = qs.stringify(values);
                        Request.conditionPopupSave(_param).then(res => {
                            if(res && res.code == REQUEST_SUCCESS){
                                this.props.showModelFunc({} , false);
                                this.props.getTableData({page: current?current-1:0, size: size(), pId: id});
                                message.success('新增成功！', 1);
                            }else{
                                res.code !== REQUEST_UNAUTHORIZED && message.error(res.msg, 2);
                            }
                        });
                    }else{//升级包条件
                        values.patchId = id;
                        values.checkpath = values.checkpath.replace(/\s*/g,"");
                        const _param = qs.stringify(values);
                        Request.conditionPatchSave(_param).then(res => {
                            if(res && res.code == REQUEST_SUCCESS){
                                this.props.showModelFunc({} , false);
                                this.props.getTableData({page: current?current-1:0, size: size(), pId: id});
                                message.success('新增成功！', 1);
                            }else{
                                res.code !== REQUEST_UNAUTHORIZED && message.error(res.msg, 2);
                            }
                        });
                    }
                }else {
                    values.id = row.id;
                    if(itemState){ //泡泡条件
                        values.popupId = id;
                        values.checkPath = values.checkPath.replace(/\s*/g,"");
                        const _param = qs.stringify(values);
                        Request.conditionPopupUpdate(_param).then(res => {
                            if(res && res.code == REQUEST_SUCCESS){
                                this.props.showModelFunc({} , false);
                                this.props.getTableData({page: current?current-1:0, size: size(), pId: id});
                                message.success('修改成功！', 1);
                            }else{
                                res.code !== REQUEST_UNAUTHORIZED && message.error(res.msg, 2);
                            }
                        });
                    }else{
                        values.patchId = id;
                        values.checkpath = values.checkpath.replace(/\s*/g,"");
                        const _param = qs.stringify(values);
                        Request.conditionPatchUpdate(_param).then(res => {
                            if(res && res.code == REQUEST_SUCCESS){
                                this.props.showModelFunc({} , false);
                                this.props.getTableData({page: current?current-1:0, size: size(), pId: id});
                                message.success('修改成功！', 1);
                            }else{
                                res.code !== REQUEST_UNAUTHORIZED && message.error(res.msg, 2);
                            }
                        });
                    }
                }
            }
        });
    }

    render(){
        const { showModelFunc, row, type } = this.props;
        const { getFieldDecorator } = this.props.form;
        let itemState = type=='popupInfoCondition';
        const title = itemState?"泡泡条件":"升级包条件"
        const formItemLayout = {
            labelCol: {
              xs: { span: 24 },
              sm: { span: 6 },
            },
            wrapperCol: {
              xs: { span: 24 },
              sm: { span: 14 },
            },
        };

        //类型数据
        const popupData = [
            {
                title: 'checkdiffervalue',
                value: 'checkdiffervalue',
            }, {
                title: 'checksamevalue',
                value: 'checksamevalue',
            }, {
                title: 'checkversion',
                value: 'checkversion',
            }, {
                title: 'checkdiffresult',
                value: 'checkdiffresult',
            }, {
                title: 'checkpopinterval',
                value: 'checkpopinterval',
            }
        ];

        const patchData = [
            {
                title: 'checkdiffervalue',
                value: 'checkdiffervalue',
            }, {
                title: 'checksamevalue',
                value: 'checksamevalue',
            }, {
                title: 'checkversion',
                value: 'checkversion',
            }, {
                title: 'checkdiffresult',
                value: 'checkdiffresult',
            }, {
                title: 'checkpopinterval',
                value: 'checkpopinterval',
            }, {
                title: 'checkpattern',
                value: 'checkpattern',
            }, {
                title: 'checknotpattern',
                value: 'checknotpattern',
            }
        ];

        return(
            <Modal 
                visible={true}
                width="50%"
                title={row.id?"修改"+title+"信息":"新增"+title+"信息"}
                footer={null}
                maskClosable={false}
                onCancel={()=>showModelFunc({} , false)}
            >
                <Form onSubmit={this.handleSubmit} >
                    <FormItem
                        {...formItemLayout}
                        label="函数"
                        >
                        {getFieldDecorator('fun',{
                            rules: [{
                                required: true, 
                                message: '请选择函数信息！'
                            }],
                            initialValue: row.fun || 'checkdiffervalue',
                        })(
                            <Select style={{ width: '100%'}} >
                                {itemState?popupData.map((item, index)=>{
                                        return <Option value={item.value} >
                                                {item.title}
                                            </Option>
                                    }):patchData.map((item, index)=>{
                                        return <Option value={item.value} >
                                                {item.title}
                                            </Option>
                                    })
                                }
                            </Select>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="检查路径"
                        >
                        {getFieldDecorator(itemState?'checkPath':'checkpath', {
                            rules: [{
                                required: true, 
                                message: '请输入检查路径！'
                            }],
                            initialValue: itemState?row.checkPath:row.checkpath || '',
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="参数"
                        >
                        {getFieldDecorator('param', {
                            initialValue: row.param || '',
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <Row type="flex" style={{ textAlign: 'center'}}>
                        <Col span={24}>
                            <FormItem>
                                <Button type="primary" htmlType="submit"  className="conform-button">保存</Button>
                                <Button type="primary" onClick={()=>showModelFunc({} , false)}>返回</Button>
                            </FormItem>
                        </Col>
                    </Row>
                </Form>

            </Modal>
        )
    }
}

export default Form.create()(ConditionTableFrom)